package com.xkdx.caipiao.redpacke;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.MyLotterActivity;
import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.application.MyApplication;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.hongbao.HongbaoAction;
import com.xkdx.caipiao.presistence.hongbao.HongbaoInfo;
import com.xkdx.caipiao.presistence.hongbao.HongbaoModule;
import com.xkdx.caipiao.presistence.hongbao.HongbaoPresistence;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;

public class RedPackeDetail extends ThreadActivity implements
		OnPageChangeListener, OnClickListener {

	ViewPager viewPager;
	TextView tv_use, tv_past;
	ListView lv_use, lv_past;
	View view_use, view_past;
	List<View> viewList;
	List<HongbaoInfo> list1;
	List<HongbaoInfo> list2;
	HongbaoAction action;
	HongbaoModule module;
	HongbaoPresistence presistence;

	HongbaoAction action2;
	HongbaoModule module2;
	HongbaoPresistence presistence2;

	Drawable im_bottom;
	String tag = "";
	int clickpage;
	int isPastRedpacket;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.red_packe_caipiao);
		im_bottom = getResources().getDrawable(R.drawable.viewpagger_line_caipiao);
		im_bottom.setBounds(0, 0, im_bottom.getMinimumWidth(),
				im_bottom.getMinimumHeight());
		tag = getIntent().getStringExtra("tag");
		findView();
		startThread();
	}

	private void findView() {
		tv_use = (TextView) findViewById(R.id.text1);
		tv_past = (TextView) findViewById(R.id.text2);

		tv_use.setOnClickListener(this);
		tv_past.setOnClickListener(this);

		viewPager = (ViewPager) findViewById(R.id.viewpager1);
		LayoutInflater lf = LayoutInflater.from(RedPackeDetail.this);

		view_use = lf.inflate(R.layout.red_listview1_caipiao, null);
		view_past = lf.inflate(R.layout.red_listview2_caipiao, null);

		viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
		viewList.add(view_use);
		viewList.add(view_past);

		lv_use = (ListView) view_use.findViewById(R.id.lv_red1);
		lv_past = (ListView) view_past.findViewById(R.id.lv_red2);

		viewPager.setAdapter(new adapter());

		viewPager.setOnPageChangeListener(this);
		if (!(TextUtils.isEmpty(tag)) && "tixian".equals(tag)) {
			viewPager.setCurrentItem(3);
		} else {
			viewPager.setCurrentItem(0);
		}

		lv_use.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (list1.get(arg2).getOid().equals("")) {
					toast("没有详细信息");
					return;
				}
				/*
				 * Intent intent = new Intent(RedPackeDetail.this,
				 * OrderDetailsActivity.class); intent.putExtra("orderid",
				 * list.get(arg2).getOid());
				 * 
				 * 
				 * startActivity(intent);
				 */
			}
		});

		lv_past.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

			}
		});
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			if (0 == isPastRedpacket) {
				list1 = module.list1;
				MyApplication.redpackageNum = list1.size();
				startThread2();
				return;
			} else if (1 == isPastRedpacket) {
				list2 = module2.list2;
			}

			if (list1 != null && list1.size() > 0 || list2 != null
					&& list2.size() > 0) {
				setList();
			} else {

			}

		} else {
			Toast.makeText(RedPackeDetail.this, "获取账户收支列表失败",
					Toast.LENGTH_SHORT).show();
		}
	}

	private void setList() {
		listAdapter adapter = new listAdapter(list1);
		lv_use.setAdapter(adapter);

		listAdapter toucaiadapter = new listAdapter(list2);
		lv_past.setAdapter(toucaiadapter);
	}

	@Override
	public void startThread() {
		isPastRedpacket = 0;
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		LoginInfo info = getLoginInfo();
		String md5 = info.getId() + info.getLogintoken() + IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action = new HongbaoAction(info.getId(), info.getLogintoken(), signcode);
		module = new HongbaoModule();
		module.flag = 0;
		action.flag = 0;

		presistence = new HongbaoPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();

	}

	public void startThread2() {
		isPastRedpacket = 1;
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		LoginInfo info = getLoginInfo();
		String md5 = info.getId() + info.getLogintoken() + IConstants.key;
		String signcode = Md5Utils.md5(md5);
		action2 = new HongbaoAction(info.getId(), info.getLogintoken(),
				signcode);

		module2 = new HongbaoModule();
		module2.flag = 1;
		action2.flag = 1;
		presistence2 = new HongbaoPresistence(this);
		presistence2.setActitons(action2);
		presistence2.setModule(module2);
		presistence2.execute();

	}

	@Override
	public void onBackClick(View view) {
		finish();
	}

	class adapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 2;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {

			container.removeView(viewList.get(position));

		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(viewList.get(position));// 添加页卡
			return viewList.get(position);
		}

	}

	class listAdapter extends BaseAdapter {
		List<HongbaoInfo> list1;
		List<HongbaoInfo> list2;

		public listAdapter(List<HongbaoInfo> list1) {
			// TODO Auto-generated constructor stub
			this.list1 = list1;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.list1.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return this.list1.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertview, ViewGroup arg2) {
			viewHolder holder;
			if (convertview == null) {
				holder = new viewHolder();
				convertview = LayoutInflater.from(RedPackeDetail.this).inflate(
						R.layout.red_list_item_caipiao, null);
				// holder.time = (TextView) convertview.findViewById(R.id.time);
				// holder.money = (TextView)
				// convertview.findViewById(R.id.money);
				// holder.type = (TextView) convertview.findViewById(R.id.type);
				holder.name = (TextView) convertview
						.findViewById(R.id.tv_red_name);
				holder.time = (TextView) convertview.findViewById(R.id.tv_time);
				holder.money = (TextView) convertview
						.findViewById(R.id.tv_red_money);

				convertview.setTag(holder);
			} else {
				holder = (viewHolder) convertview.getTag();
			}

			float mon = Float.valueOf(list1.get(position).getFacevalue()) / 100;
			DecimalFormat df = new DecimalFormat("#0.00");
			String facevalue = df.format(mon);

			holder.money.setText(facevalue + "元");
			holder.name.setText(list1.get(position).getDetail());
			holder.time.setText(list1.get(position).getValidity());
			String status = list1.get(position).getStatus();

			/*
			 * holder.money.setText(money + "元");
			 * holder.time.setText(list.get(position).getCtime()); String status
			 * = list.get(position).getType(); String detail =
			 * list.get(position).getDetail();
			 * 
			 * if (!("".equals(detail))) { holder.type.setText(detail); } else
			 * if (status.equals("1")) { holder.type.setText("购彩"); }
			 */
			return convertview;
		}

		class viewHolder {
			public TextView name;
			TextView time, money, type;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int id) {
		switch (id) {
		case 0:
			tv_use.setCompoundDrawables(null, null, null, im_bottom);
			tv_past.setCompoundDrawables(null, null, null, null);

			break;
		case 1:
			tv_use.setCompoundDrawables(null, null, null, null);
			tv_past.setCompoundDrawables(null, null, null, im_bottom);

		default:
			break;
		}
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.text1:
			clickpage = 0;
			viewPager.setCurrentItem(0);
			break;
		case R.id.text2:
			clickpage = 1;
			viewPager.setCurrentItem(1);
			break;

		}
	}

}
